<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="公告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="daterangeSyncTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['community:notice:export']"-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="公告ID" align="center" prop="noticeId" width="100"/>-->
      <el-table-column type="index" width="50" />
      <el-table-column label="公告标题" align="center" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column label="创建者" align="center" prop="author" width="80"/>
      <el-table-column label="创建时间" align="center" prop="syncTime" width="180">
      </el-table-column>
      <el-table-column label="阅读量" align="center" prop="viewNumber" width="80"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['community:notice:edit']"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            v-hasPermi="['community:notice:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['community:notice:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" height="250"/>
<!--          <editor v-model="form.content" :min-height="192" style="max-height: 350px; overflow-y: scroll"/>-->
        </el-form-item>
        <el-form-item label="是否置顶">
          <el-radio-group v-model="form.topStatus">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看信息公告对话框 -->
    <el-dialog :title="title" :visible.sync="noticeDetailDialogOpen" width="650px" append-to-body :before-close="cancelView">
      <el-skeleton style="width: 600px" :loading="noticeContentLoading" animated>
        <template #template>
<!--          <el-skeleton-item variant="image" style="width: 240px; height: 240px" />-->
          <div style="padding: 14px">
            <el-skeleton :rows="5" animated />
          </div>
        </template>
        <template #default>
          <div class="noticeTitle">
            {{ form.title }}
          </div>
          <div style="text-align: center">
            <div style="display: inline-block; margin: 0 auto">
              <span class="time">发布者：{{ form.author }}</span>
              <span class="time">发布时间：{{ form.syncTime }}</span>
              <span class="time">阅读量：{{ form.viewNumber }}</span>
            </div>
          </div>
          <div class="noticeContent ql-snow ql-editor" v-html="form.content">
          </div>
        </template>
      </el-skeleton>
<!--      <div class="noticeTitle">-->
<!--        {{ form.title }}-->
<!--      </div>-->
<!--      <div style="text-align: center">-->
<!--        <div style="display: inline-block; margin: 0 auto">-->
<!--          <span class="time">发布者：{{ form.author }}</span>-->
<!--          <span class="time">发布时间：{{ form.syncTime }}</span>-->
<!--          <span class="time">阅读量：{{ form.viewNumber }}</span>-->
<!--        </div>-->
<!--      </div>-->
<!--      <div class="noticeContent ql-snow ql-editor" v-html="form.content">-->
<!--      </div>-->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelView">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, previewNoticeWithoutPic, getNotice, delNotice, addNotice, updateNotice } from '@/api/cpc/notice'
import Editor from '@/components/Editor'
import { dateFormat } from '@/utils/dateUtils'

export default {
  name: 'Notice',
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 控制加载通知内容的遮罩层
      noticeContentLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息公告表格数据
      noticeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      noticeDetailDialogOpen: false,
      // 创建时间时间范围
      daterangeSyncTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        syncTime: null,
        communityId: null
      },
      // 表单参数
      form: {
        topStatus: 0,
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: '公告标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '公告创建人不能为空', trigger: 'blur' }
        ],
        syncTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        viewNumber: [
          { required: true, message: '阅读量不能为空', trigger: 'blur' }
        ],
        communityId: [
          { required: true, message: '社区id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询信息公告列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.daterangeSyncTime != null && this.daterangeSyncTime !== '') {
        this.queryParams.params['beginSyncTime'] = this.daterangeSyncTime[0]
        this.queryParams.params['endSyncTime'] = this.daterangeSyncTime[1]
      }
      previewNoticeWithoutPic(this.queryParams).then(response => {
        this.noticeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.noticeDetailDialogOpen = false
      this.noticeContentLoading = false
      this.reset()
    },
    // 关闭公告详情页面
    cancelView() {
      this.noticeDetailDialogOpen = false
      this.noticeContentLoading = false
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: null,
        title: null,
        content: null,
        topStatus: 0,
        author: null,
        syncTime: null,
        viewNumber: null,
        communityId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSyncTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加信息公告'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改信息公告'
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.noticeDetailDialogOpen = true
      this.noticeContentLoading = true
      this.reset()
      const noticeId = row.noticeId
      getNotice(noticeId).then(response => {
        this.form = response.data
        this.title = '信息公告详情'
        this.noticeContentLoading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.noticeId != null) {
            updateNotice(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            const createTime = new Date()
            this.form.syncTime = dateFormat('YYYY-mm-dd HH:MM:SS', createTime)
            // console.log('tttttttttttttttttttttt', this.form.syncTime)
            addNotice(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$confirm('是否确认删除信息公告编号为"' + noticeIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delNotice(noticeIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    // /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams
    //   this.$confirm('是否确认导出所有信息公告数据项?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function() {
    //     return exportNotice(queryParams)
    //   }).then(response => {
    //     this.download(response.msg)
    //   })
    // }
  }
}
</script>

<style>
.noticeTitle {
  font-size: 24px;
  color: #444444;
  text-align: center;
  font-weight: bold;
  line-height: 38px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ddd;
  margin-bottom: 20px;
}
.time {
  color: #999;
  font-size: 12px;
  line-height: 20px;
  height: 20px;
  margin-left: 5px;
  margin-right: 5px;
}
.noticeContent{
  font-size: 16px;
  word-wrap: break-word;
  table-layout: fixed;
  word-break: break-all;
  max-height: 350px;
  overflow-y: scroll;
}
.noticeContent::-webkit-scrollbar {
  display:none
}
.noticeContent img{
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  display: block;
  margin: 0 auto;
}
</style>
